﻿using DeZhen.Core.Consul.Client;
using DeZhen.Core.Consul.Entity;
using DeZhen.Core.Framework.Business.Ioc;
using DeZhen.Core.Framework.Entity;
using DeZhen.Core.Framework.Http;
using DeZhen.Core.Redis.Entity;
using DeZhen.Core.Template.Domain.Entity.TableEntity;
using DeZhen.Core.Template.Domain.Service.IService;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;

namespace DeZhen.Core.Template.Web.Controllers
{
    [Route("api/[controller]/[action]")]
    public class DemoController : BaseApiController
    {
        /// <summary>
        /// 直接通过[Autowired]即可注入对象，无需写臃肿的构造函数代码或IOC容器获取代码,简洁明了
        /// </summary>
        [Autowired]
        private IUserService _userService;
        /// <summary>
        /// 同一接口的不通实现可以传入不同服务的Identifier即可获取不同的实现
        /// </summary>
        [Autowired("web")]
        private ILoginService _webLoginService;
        [Autowired("dingding")]
        private ILoginService _dingTalkbLoginService;
        [Autowired("wechat")]
        private ILoginService _weChatLoginService;
        [Autowired]
        private IConsulHttpClient _consulHttpClient;
        /// <summary>
        /// 通过特性即可读取配置文件
        /// </summary>
        [Value("Redis")]
        private RedisConfig Redis;
        [Value("Consul")]
        private ConsulConfig Consul;
        /// <summary>
        /// 当前版本还需要此构造函数代码,以后版本会进行舍去
        /// </summary>
        /// <param name="autowiredService"></param>
        public DemoController(IAutowiredPlatform autowiredService)
        {
            autowiredService.Autowired(this);
        }
        /// <summary>
        /// 演示负载均衡,返回信息是请求的不同的实例
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<HttpResult> ShwoRoundRobinDemo()
        {
            String serviceName = "Demo";
            return HttpResult.Success(await _consulHttpClient.GetAsync($"http://{serviceName}/api/demo/GetSelfAddress"));
        }
        /// <summary>
        /// 演示数据库查询操作
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<HttpResult> GetUserInfoFromDataBase()
        {
            return HttpResult.Success(await _userService.GetUserInfoFromDataBaseAsync());
        }
        /// <summary>
        /// 演示Redis查询操作
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<HttpResult> GetUserInfoFromRedis(String userId)
        {
            return HttpResult.Success(await _userService.GetUserInfoFromRedisAsync(userId));
        }
        /// <summary>
        /// 演示读取配置文件
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public HttpResult GetConfig()
        {
            return HttpResult.Success(Redis);
        }
        /// <summary>
        /// 演示数据库新增操作
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<HttpResult> CreateUserInfoToDataBase(UserInfo userInfo)
        {
            return HttpResult.Success(await _userService.CreateUserInfoToDataBase(userInfo));
        }
        /// <summary>
        /// 演示Redis新增操作
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<HttpResult> CreateUserInfoToRedis(UserInfo userInfo)
        {
            return HttpResult.Success(await _userService.CreateUserInfoToRedis(userInfo.UserId.ToString(), userInfo));
        }
        /// <summary>
        /// 演示同一接口的不同实现类的获取
        /// </summary>
        /// <param name="identifier"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<HttpResult> GetImplByIdentifier(String identifier)
        {
            switch(identifier)
            {
                case "web":
                    return HttpResult.Success(await _webLoginService.LoginAsync());
                case "dingding":
                    return HttpResult.Success(await _dingTalkbLoginService.LoginAsync());
                case "wechat":
                    return HttpResult.Success(await _weChatLoginService.LoginAsync());
                default: throw new NotImplementedException();
            }
        }
        [HttpGet]
        public String GetSelfAddress()
        {
            return Consul.GatewayAddress;
        }
    }
}
